// Copyright 2025 piko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// 基础API配置
const BASE_URL = process.env.NEXT_PUBLIC_BASE_API_URL || 'http://localhost:3001/api/';

// 示例图片资源
export const imageSrcs = [
  { id: '1', src: 'https://placedog.net/200/200' },
  { id: '2', src: 'https://placebear.com/200/200' },
  { id: '3', src: 'https://picsum.photos/200/200' },
];

/**
 * 认证相关API接口
 */
export const authApi = {
  // 基础URL
  BASE_URL,
  // 管理员登录接口
  LOGIN: `${BASE_URL}/auth/admin/login`,
  // 登出接口
  LOGOUT: `${BASE_URL}/auth/logout`,
  // 获取当前用户信息
  CURRENT_USER: `${BASE_URL}/admin/users/me`,
  // 刷新令牌
  REFRESH_TOKEN: `${BASE_URL}/auth/refresh`,
};

/**
 * 仪表盘相关API接口
 */
export const dashboardApi = {
  // 获取仪表盘统计数据
  DASHBOARD: `${BASE_URL}/admin/common/dashboard`,
  // 获取活动日志
  ACTIVITY_LOGS: `${BASE_URL}/admin/common/latest-logs`,
  // 获取热门文章
  HOT_ARTICLES: `${BASE_URL}/admin/common/hot-articles`,
  // 获取系统状态
  SYSTEM_STATUS: `${BASE_URL}/admin/common/system-status`,
};

/**
 * 用户管理相关API接口
 */
export const userApi = {
  // 用户列表
  LIST: `${BASE_URL}/admin/users`,
  // 获取用户详情
  DETAIL: (id: number) => `${BASE_URL}/admin/users/${id}`,
  // 创建用户
  CREATE: `${BASE_URL}/admin/users`,
  // 更新用户信息
  UPDATE: (id: number) => `${BASE_URL}/admin/users/${id}`,
  // 更新用户状态
  UPDATE_STATUS: (id: number) => `${BASE_URL}/admin/users/status/${id}`,
  // 删除用户
  DELETE: (id: number) => `${BASE_URL}/admin/users/${id}`,
  // 获取用户操作日志
  USER_LOGS: (userId: number) => `${BASE_URL}/admin/users/${userId}/logs`,
  // 重置用户密码
  RESET_PASSWORD: (id: number) => `${BASE_URL}/admin/users/${id}/reset-password`,
  // 批量操作（禁用/删除等）
  BATCH_OPERATE: `${BASE_URL}/admin/users/batch-operate`,     
};

/**
 * 文章管理相关API接口
 */
export const articleApi = {
  // 文章列表
  LIST: `${BASE_URL}/admin/articles`,
  // 获取文章详情
  DETAIL: (id: number) => `${BASE_URL}/admin/articles/${id}`,
  // 创建文章
  CREATE: `${BASE_URL}/admin/articles`,
  // 更新文章
  UPDATE: (id: number) => `${BASE_URL}/admin/articles/${id}`,
  // 删除文章
  DELETE: (id: number) => `${BASE_URL}/admin/articles/${id}`,
  // 更新文章状态
  UPDATE_STATUS: (id: number) => `${BASE_URL}/admin/articles/${id}/status`,
  // 批量操作
  BATCH_OPERATE: `${BASE_URL}/admin/articles/batch-operate`,
  // 上传文章封面
  UPLOAD_COVER: `${BASE_URL}/admin/articles/upload-cover`,
  // 获取文章统计
  STATS: `${BASE_URL}/admin/articles/stats`,
};

/**
 * 评论管理相关API接口
 */
export const commentApi = {
  // 评论列表
  LIST: `${BASE_URL}/admin/comments`,
  // 获取评论详情
  DETAIL: (id: number) => `${BASE_URL}/admin/comments/${id}`,
  // 删除评论
  DELETE: (id: number) => `${BASE_URL}/admin/comments/${id}`,
  // 更新评论状态
  UPDATE_STATUS: (id: number) => `${BASE_URL}/admin/comments/${id}/status`,
  // 批量操作
  BATCH_OPERATE: `${BASE_URL}/admin/comments/batch-operate`,
  // 获取评论统计
  STATS: `${BASE_URL}/admin/comments/stats`,
  // 获取评论回复列表
  REPLIES: (commentId: number) => `${BASE_URL}/admin/comments/${commentId}/replies`,
  // 回复评论
  REPLY: (commentId: number) => `${BASE_URL}/admin/comments/${commentId}/reply`,
  // 审核评论
  REVIEW: (id: number) => `${BASE_URL}/admin/comments/${id}/review`,
  // 屏蔽评论者
  BLOCK_USER: (userId: number) => `${BASE_URL}/admin/comments/block-user/${userId}`,
  // 敏感词管理
  SENSITIVE_WORDS: `${BASE_URL}/admin/sensitive/sensitive-words`,
  SENSITIVE_WORD_DETAIL: (id: number) => `${BASE_URL}/admin/sensitive/sensitive-words/${id}`,
  SENSITIVE_WORD_STATUS: (id: number) => `${BASE_URL}/admin/sensitive/sensitive-words/${id}/status`,
  SENSITIVE_WORDS_BATCH: `${BASE_URL}/admin/sensitive/sensitive-words/batch`,
};

/**
 * 分类管理相关API接口
 */
export const categoryApi = {
  // 分类列表
  LIST: `${BASE_URL}/admin/categories`,
  // 获取分类详情
  DETAIL: (id: number) => `${BASE_URL}/admin/categories/${id}`,
  // 创建分类
  CREATE: `${BASE_URL}/admin/categories`,
  // 更新分类
  UPDATE: (id: number) => `${BASE_URL}/admin/categories/${id}`,
  // 删除分类
  DELETE: (id: number) => `${BASE_URL}/admin/categories/${id}`,
  // 更新分类状态
  UPDATE_STATUS: (id: number) => `${BASE_URL}/admin/categories/${id}/status`,
  // 更新分类排序
  UPDATE_ORDER: `${BASE_URL}/admin/categories/update-order`,
};

/**
 * 标签管理相关API接口
 */
export const tagApi = {
  // 标签列表
  LIST: `${BASE_URL}/admin/tags`,
  // 获取标签详情
  DETAIL: (id: number) => `${BASE_URL}/admin/tags/${id}`,
  // 创建标签
  CREATE: `${BASE_URL}/admin/tags`,
  // 更新标签
  UPDATE: (id: number) => `${BASE_URL}/admin/tags/${id}`,
  // 删除标签
  DELETE: (id: number) => `${BASE_URL}/admin/tags/${id}`,
  // 更新标签状态
  UPDATE_STATUS: (id: number) => `${BASE_URL}/admin/tags/${id}/status`,
  // 批量操作
  BATCH_OPERATE: `${BASE_URL}/admin/tags/batch-operate`,
  // 获取热门标签
  POPULAR: `${BASE_URL}/admin/tags/popular`,
};

/**
 * 点赞管理相关API接口
 */
export const likeApi = {
  // 点赞列表
  LIST: `${BASE_URL}/admin/likes`,
  // 获取点赞详情
  DETAIL: (id: number) => `${BASE_URL}/admin/likes/${id}`,
  // 删除点赞记录
  DELETE: (id: number) => `${BASE_URL}/admin/likes/${id}`,
  // 批量操作
  BATCH_OPERATE: `${BASE_URL}/admin/likes/batch-operate`, 
};

/**
 * 系统日志相关API接口
 */
export const logApi = {
  // 管理员操作日志
  ADMIN_OPERATIONS: `${BASE_URL}/admin/logs/admin-operations`,
  // 登录日志
  LOGIN_LOGS: `${BASE_URL}/admin/logs/login-logs`,
  // 安全事件
  SECURITY_EVENTS: `${BASE_URL}/admin/logs/security-events`,
  // 标记安全事件已读
  MARK_SECURITY_READ: (id: number) => `${BASE_URL}/admin/logs/security-events/${id}/read`,
  // 解决安全事件
  RESOLVE_SECURITY: (id: number) => `${BASE_URL}/admin/logs/security-events/${id}/resolve`,
  // 日志统计
  STATISTICS: `${BASE_URL}/admin/logs/statistics`,
  // 清理过期日志
  CLEANUP: `${BASE_URL}/admin/logs/cleanup`,
  
  // 兼容旧的通用接口
  LIST: `${BASE_URL}/admin/logs`,
  DETAIL: (id: number) => `${BASE_URL}/admin/logs/${id}`,
  DELETE: (id: number) => `${BASE_URL}/admin/logs/${id}`,
  CLEAR: `${BASE_URL}/admin/logs/clear`,
  BATCH_OPERATE: `${BASE_URL}/admin/logs/batch-operate`,
};

/**
 * 系统配置相关API接口
 */
export const systemConfigApi = {
  // 获取系统配置
  GET_CONFIG: `${BASE_URL}/admin/system/config`,
  // 更新系统配置
  UPDATE_CONFIG: `${BASE_URL}/admin/system/config`,
  // 获取系统信息
  GET_SYSTEM_INFO: `${BASE_URL}/admin/system/info`,
  // 获取服务器状态
  GET_SERVER_STATUS: `${BASE_URL}/admin/system/server-status`,
};

/**
 * 上传相关API接口
 */
export const uploadApi = {
  // 上传图片
  UPLOAD_IMAGE: `${BASE_URL}/admin/upload/image`,
  // 上传文件
  UPLOAD_FILE: `${BASE_URL}/admin/upload/file`,
  // 获取上传文件列表
  GET_FILES: `${BASE_URL}/admin/upload/files`,
  // 删除文件
  DELETE_FILE: (id: number) => `${BASE_URL}/admin/upload/files/${id}`,
};


// 导出所有API集合，方便统一管理
export const api = {
  auth: authApi,
  dashboard: dashboardApi,
  user: userApi,
  article: articleApi,
  comment: commentApi,
  category: categoryApi,
  tag: tagApi,
  like: likeApi,
  log: logApi,
  system: systemConfigApi,
  upload: uploadApi,
};

// 为了兼容旧代码，保留原始的apiUser对象
export const apiUser = {
  BaseUrl: BASE_URL,
  apiLoginUrl: authApi.LOGIN,
  apiLogoutUrl: authApi.LOGOUT,
  apiUserInfoUrl: authApi.CURRENT_USER,
  apiUserListUrl: userApi.LIST,
  info: authApi.CURRENT_USER,
  list: userApi.LIST,
};
